package models.application;

import java.util.List;

import javax.persistence.*;

import models.pubobj.*;

import models.user.AniUser;
import play.db.ebean.Model;

@Entity
public class AniUserAppsRel extends Model {

	@Id
	public Long id;

    @ManyToOne
    public AniUser clientUser;

	@ManyToOne
	public AniApp clientApp;
	
	@ManyToOne
	public AniApp targetApp;
	
	@ManyToOne
	public AniObjectState state;

    public AniUserAppsRel(AniUser clientUser, AniApp clientApp, AniApp targetApp, AniObjectState state) {
        this.clientUser = clientUser;
        this.clientApp = clientApp;
        this.targetApp = targetApp;
        this.state = state;
    }

    public static Model.Finder<Long, AniUserAppsRel> find = new Model.Finder(Long.class, AniUserAppsRel.class);
	
	public static AniUserAppsRel withUserAndClientTargetAppObjs(AniUser curUser, AniApp clientApp, AniApp targetApp){
		return find.where()
                .eq("clientApp.id", clientApp.id)
                .eq("targetApp.id", targetApp.id)
                .eq("clientUser.id", curUser.id)
                .findUnique();
	}

    public static Integer findRowCountWithUserAndClientTargetAppObjs(AniUser curUser, AniApp clientApp, AniApp targetApp){
        return find.where()
                .eq("clientApp.id", clientApp.id)
                .eq("targetApp.id", targetApp.id)
                .eq("clientUser.id", curUser.id)
                .findRowCount();
    }
	
	public static List<AniUserAppsRel> findAll() {
        return find.all();
    }
}
